/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Creditos;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author alrojas
 */
public class IDetalle extends JFrame{
    
    private JLabel      clienteLab;
    private JTextField  clienteCam;
    //private JButton     clienteBut;
    private JTextField  cliente2Cam;
    private JTable      listaTab;
    private DefaultTableModel modeloTab;
    private TableRowSorter<TableModel> clasificador;
    
    private JLabel interesesLab;
    private JLabel moraLab;
    //private JLabel montoTotalLab;
    //private JLabel nroCuotaslLab;
    
    private JTextField interesesTex;
    private JTextField moraTex;
    //private JTextField montoTotalTex;
    //private JTextField nroCuotaslTex;
    
    private JButton pagarBut;
    //private JButton generarBut;
    private JButton descartarBut;
    
    public IDetalle(int credito){
        this.setLayout(new GridBagLayout());
        this.setTitle("Detalle Crédito");
        
        inicializarComponentes(credito);
        inicializarListeners(credito);
        
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void inicializarComponentes(int credito){
        
        GridBagConstraints limites = new GridBagConstraints();
        limites.insets = new Insets(2, 2, 2, 2);
        
        limites.fill = GridBagConstraints.HORIZONTAL;
        
        Vector < String > info = null;
        try {
            info = CCredito.dameInfo(credito);
        } catch (SQLException ex) {
            Logger.getLogger(IDetalle.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IDetalle.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        JPanel panelCliente = new JPanel(new FlowLayout());
        clienteLab = new JLabel("Cliente: "); 
        clienteCam = new JTextField(info.elementAt(0),10); /*clienteCam.setText(padreReal.getCodigoCliente());*/ clienteLab.setLabelFor(clienteCam); clienteCam.setHorizontalAlignment(JLabel.CENTER); clienteCam.setEditable(false);
        cliente2Cam = new JTextField(info.elementAt(1),42); /*cliente2Cam.setText(padreReal.getNombreCliente());*/ clienteLab.setLabelFor(clienteCam); cliente2Cam.setHorizontalAlignment(JLabel.CENTER); cliente2Cam.setEditable(false);
        panelCliente.add(clienteLab); panelCliente.add(clienteCam); panelCliente.add(cliente2Cam); //panelCliente.add(clienteBut);
        limites.gridx = 0; limites.gridy = 0;
        add(panelCliente,limites);
        
        limites.fill = GridBagConstraints.CENTER;
        
        JPanel panelCredito = new JPanel(new GridLayout(1,2));
        interesesLab = new JLabel("Intéres (%): "); interesesLab.setHorizontalAlignment(JLabel.RIGHT);
        moraLab = new JLabel("Mora (%): "); moraLab.setHorizontalAlignment(JLabel.RIGHT);
        interesesTex = new JTextField(info.elementAt(3),12); interesesTex.setHorizontalAlignment(JTextField.RIGHT); interesesTex.setEditable(false);
        moraTex = new JTextField(info.elementAt(3),12); moraTex.setHorizontalAlignment(JTextField.RIGHT); moraTex.setEditable(false);
        panelCredito.add(interesesLab); panelCredito.add(interesesTex); panelCredito.add(moraLab); panelCredito.add(moraTex);
        limites.gridy = 1;
        add(panelCredito,limites);
        
        DefaultTableCellRenderer rendererCentro = new DefaultTableCellRenderer();
        rendererCentro.setHorizontalAlignment(JLabel.CENTER);
        
        DefaultTableCellRenderer rendererDerecho = new DefaultTableCellRenderer();
        rendererDerecho.setHorizontalAlignment(JLabel.RIGHT);

        Vector<String> columnNames = new Vector<String>(); columnNames.add("Codigo"); columnNames.add("Monto Cuota"); columnNames.add("Interes"); columnNames.add("Monto Final"); columnNames.add("F. Vencimiento"); columnNames.add("");     
        Vector < Vector > cuotas = null;
        try {
            cuotas = CCredito.dameCuotas(credito);
        } catch (SQLException ex) {
            Logger.getLogger(IDetalle.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IDetalle.class.getName()).log(Level.SEVERE, null, ex);
        }
        modeloTab = new DefaultTableModel(cuotas,columnNames){
            @Override
            public boolean isCellEditable (int row, int column)
            {
                if(column == 5)
                    return true;
                return false;
            }  
            
            @Override
            public Class getColumnClass(int column) {
                Class []classes = {String.class,String.class,String.class,String.class,String.class,Boolean.class};
/*                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return String.class;
                    case 3:
                        return String.class;
                    case 4:
                        return String.class;
                    case 5:
                        return Boolean.class;
                }*/
                return classes[column];
            }
        };

            
        this.clasificador = new TableRowSorter<TableModel>(modeloTab);
        listaTab = new JTable(modeloTab); listaTab.setRowHeight(20); listaTab.setRowSorter(clasificador); listaTab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ((DefaultTableCellRenderer)listaTab.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
        listaTab.getColumnModel().getColumn(0).setCellRenderer(rendererCentro);
        listaTab.getColumnModel().getColumn(1).setCellRenderer(rendererCentro);
        listaTab.getColumnModel().getColumn(2).setCellRenderer(rendererCentro);
        listaTab.getColumnModel().getColumn(3).setCellRenderer(rendererCentro); 
        listaTab.getColumnModel().getColumn(4).setCellRenderer(rendererCentro);
        listaTab.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(new JCheckBox()));
        
        limites.gridy = 2; //limites.gridwidth = 4;
        JScrollPane barras = new JScrollPane(listaTab); barras.setPreferredSize(new Dimension(600, 150));
        add(barras,limites);
        
        JPanel panelBotones = new JPanel(new FlowLayout());
        pagarBut = new JButton("Pagar Seleccionados", new ImageIcon("figuras/payment32.png"));
        pagarBut.setHorizontalTextPosition(SwingConstants.CENTER);
        pagarBut.setVerticalTextPosition(SwingConstants.BOTTOM) ;
        pagarBut.setVerticalAlignment(SwingConstants.CENTER);

        descartarBut = new JButton("Descartar", new ImageIcon("figuras/trash32.png"));
        descartarBut.setHorizontalTextPosition(SwingConstants.CENTER);
        descartarBut.setVerticalTextPosition(SwingConstants.BOTTOM) ;
        descartarBut.setVerticalAlignment(SwingConstants.CENTER);
        limites.gridy = 3; //limites.gridwidth = 3;
        panelBotones.add(pagarBut); /*panelBotones.add(generarBut);*/ panelBotones.add(descartarBut);
        add(panelBotones,limites);
         
    }
    
    public void inicializarListeners(final int codigo){
        
        pagarBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                if(/*listaTab.getRowCount() > 0*/ true){
                    try {
                        CCredito.pagarCuotas(codigo,modeloTab.getDataVector());
                        
                        setVisible(false);
                    } catch (SQLException ex) {
                        Logger.getLogger(IDetalle.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(IDetalle.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                }
                else{
                    JOptionPane.showMessageDialog(null,"No hay cuotas seleccionadas!","Aviso",JOptionPane.WARNING_MESSAGE); 
                }
                

            }
        });
        
        descartarBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
    
    

}
